﻿Imports System.Data.SqlClient

Public Class AddUnit

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub AddUnit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = Config.lblselectedprofile.Text
        txtsfdcip.Focus()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If txtsfdcip.Text.Trim <> "" And txtdevice.Text.Trim <> "" And txtdesc.Text.Trim <> "" Then
            If ExistUnit(txtdesc.Text.Trim) = False Then
                Dim cnn As New SqlConnection("Data Source=143.116.204.27;Initial Catalog=Emulator;Persist Security Info=True;User ID=gerardo_martinez;Password=Sanmina01")
                Dim cmd As New SqlCommand("INSERT into emu_configs(id_perfil, nombre, ip, device) values (@id, @name ,@ip ,@unit)", cnn)

                cmd.Parameters.AddWithValue("@id", GetID(Me.Text))
                cmd.Parameters.AddWithValue("@name", txtdesc.Text.Trim)
                cmd.Parameters.AddWithValue("@ip", txtsfdcip.Text.Trim)
                cmd.Parameters.AddWithValue("@unit", txtdevice.Text.Trim)

                Using cnn
                    cnn.Open()
                    cmd.ExecuteNonQuery()
                    cnn.Close()
                End Using

                Config.Actualizar()
                MsgBox("The unit " + txtdesc.Text.Trim + " has been added succesfully to profile " + Me.Text + "!", MsgBoxStyle.OkOnly, "Unit added!!")

                txtdesc.Text = ""
                txtdevice.Text = ""
                txtsfdcip.Text = ""


            Else
                MsgBox("The Unit name '" + txtdesc.Text.Trim + "' already exist!", MsgBoxStyle.Critical, "Profile alert!")
            End If
        Else
            MsgBox("There should be no empty fields!", MsgBoxStyle.OkOnly, "Unit alert!")
        End If
        txtsfdcip.Focus()
    End Sub

    Private Function GetID(ByVal name As String) As Integer
        Dim cnn As New SqlConnection("Data Source=143.116.204.27;Initial Catalog=Emulator;Persist Security Info=True;User ID=gerardo_martinez;Password=Sanmina01")
        Dim cmd As New SqlCommand("SELECT id_perfil FROM perfiles where nombre = @name", cnn)
        cmd.Parameters.AddWithValue("@name", name)
        Using cnn
            cnn.Open()
            Return cmd.ExecuteScalar
            cnn.Close()
        End Using
    End Function

    Public Function ExistUnit(ByVal name As String) As Boolean
        Dim cnn As New SqlConnection("Data Source=143.116.204.27;Initial Catalog=Emulator;Persist Security Info=True;User ID=gerardo_martinez;Password=Sanmina01")
        Dim cmd As New SqlCommand("select count(*) from emu_configs where (nombre = @nombre)", cnn)
        Dim result As Integer
        cmd.Parameters.AddWithValue("@nombre", name)
        Using cnn
            cnn.Open()
            result = cmd.ExecuteScalar()
            cnn.Close()
            If result > 0 Then
                Return True
            Else
                Return False
            End If
        End Using
    End Function

End Class